package com.ustarlink.live.net;

import android.content.Context;
import android.os.AsyncTask;

import com.ustarlink.live.channel.ChannelOpenHelper;
import com.ustarlink.live.channel.ChannelXPPAndSQLite;
import com.ustarlink.live.MainActivity;

import java.io.FileNotFoundException;

/**
 * Created by cfm on 16-1-22.
 */
public class EPGDownloadTask extends AsyncTask<Void, Void, Void> {

    private MainActivity mainActivity;

    public EPGDownloadTask(MainActivity mainActivity) {
        this.mainActivity = mainActivity;
    }

    @Override
    protected Void doInBackground(Void... arg0) {
        //Download the file
        try {
            Downloader.DownloadFromURL(mainActivity.getApplicationContext(),
                    "http://www.cfm880.com/wp-content/uploads/2015/12/tvs_epg.xml",
                    mainActivity.openFileOutput("tvs_epg.xml", Context.MODE_PRIVATE));
            mainActivity.getApplicationContext().deleteDatabase(ChannelOpenHelper.DATABASE_NAME);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        return null;
    }

    // 写入数据库
    @Override
    protected void onPostExecute(Void aVoid) {

        ChannelXPPAndSQLite.writeToDatabase(mainActivity);

    }
}
